import { currentUser_data } from './mock_data/user';
import _ from 'lodash';
const waitTime = (time = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

async function getFakeCaptcha(req, res) {
  await waitTime(2000);
  return res.json('captcha-xxx');
}

export default {
  // 支持值为 Object 和 Array

  'POST /api/user/login': (req, res) => {
    const { username, password } = req.body;
    if (username == 'admin' && password == 'admin') {
      res.send({
        code: 200,
        message: '成功!',
        data: {
          id: 1,
          username: 'admin',
          user: {
            username: '1233333333'
          },
          token:
            'eyJraWQiOiIxIiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJpc3MiOiJhdXRoMCIsImV4cCI6MTY3NzQwNzU5NiwidXNlcm5hbWUiOiJhZG1pbiJ9.M_VLdRXuR8kQl5AmU0TahGhJZ7UL2tN5FXFwdfOTiJ8',
        },
      });
      return;
    }
    if (username == 'user' && password == 'user') {
      res.send({
        code: '200',
        message: '成功!',
        data: {
          id: 1,
          username: 'user',
          user: {
            username: '1233333333'
          },
          token:
            'eyJraWQiOiIxIiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJpc3MiOiJhdXRoMCIsImV4cCI6MTY3NzQwNzU5NiwidXNlcm5hbWUiOiJhZG1pbiJ9.M_VLdRXuR8kQl5AmU0TahGhJZ7UL2tN5FXFwdfOTiJ8',
        },
      });
      return;
    }
    res.send({
      code: 0,
      message: '密码或账号错误',
      data: null,
    });
    return;
  },

  'GET /api/user/loginOut': (req, res) => {
    res.send({
      code: 200,
      data: {},
    });
    return;
  },

  'GET /api/user/currentUser': (req, res) => {
    const { token } = req.headers;
    if (token != 'null' && token != null && token != undefined && token != '') {
      res.send({
        code: 200,
        message: '成功',
        token: token,
        data: currentUser_data,
      });
      return;
    } else {
      res.send({
        code: 500,
        token: token,
        message: '失败',
      });
      return;
    }
  },

  'GET /api/getDict': (req, res) => {
    res.send({
      code: 200,
      message: '获取字典成功',
      data: {
        username: '用户名',
        password: '密码',
      },
    });
    return;
  },
  /************************************************************************************ */
  // GET POST 可省略
  'GET /api/users': [
    {
      key: '1',
      name: 'John Brown',
      age: 32,
      address: 'New York No. 1 Lake Park',
    },
    {
      key: '2',
      name: 'Jim Green',
      age: 42,
      address: 'London No. 1 Lake Park',
    },
    {
      key: '3',
      name: 'Joe Black',
      age: 32,
      address: 'Sidney No. 1 Lake Park',
    },
  ],
  'POST /api/login/account': async (req, res) => {
    const { password, username, type } = req.body;
    await waitTime(1000);
    if (password === 'ant.design' && username === 'admin') {
      res.send({
        status: 'ok',
        type,
        currentAuthority: 'admin',
      });
      access = 'admin';
      return;
    }
    if (password === 'ant.design' && username === 'user') {
      res.send({
        status: 'ok',
        type,
        currentAuthority: 'user',
      });
      access = 'user';
      return;
    }
    if (type === 'mobile') {
      res.send({
        status: 'ok',
        type,
        currentAuthority: 'admin',
      });
      access = 'admin';
      return;
    }

    res.send({
      status: 'error',
      type,
      currentAuthority: 'guest',
    });
    access = 'guest';
  },
  'POST /api/login/outLogin': (req, res) => {
    access = '';
    res.send({ data: {}, success: true });
  },
  'POST /api/register': (req, res) => {
    res.send({ status: 'ok', currentAuthority: 'user', success: true });
  },
  'GET /api/500': (req, res) => {
    res.status(500).send({
      timestamp: 1513932555104,
      status: 500,
      error: 'error',
      message: 'error',
      path: '/base/category/list',
    });
  },
  'GET /api/404': (req, res) => {
    res.status(404).send({
      timestamp: 1513932643431,
      status: 404,
      error: 'Not Found',
      message: 'No message available',
      path: '/base/category/list/2121212',
    });
  },
  'GET /api/403': (req, res) => {
    res.status(403).send({
      timestamp: 1513932555104,
      status: 403,
      error: 'Forbidden',
      message: 'Forbidden',
      path: '/base/category/list',
    });
  },
  'GET /api/401': (req, res) => {
    res.status(401).send({
      timestamp: 1513932555104,
      status: 401,
      error: 'Unauthorized',
      message: 'Unauthorized',
      path: '/base/category/list',
    });
  },

  'GET  /api/login/captcha': getFakeCaptcha,
};
